package com.xiaoa.gossip.cluster.utils;

import com.xiaoa.gossip.cluster.command.PingCommand;
import com.xiaoa.gossip.cluster.command.PingNodeState;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * 集群工具类
 *
 * @author luokaichuang
 * @time 2020/5/9
 */
public class ClusterUtils {

    /**
     * ping 对象转为PingNodeState
     *
     * @author jm008783
     * @param: [pingCommand]
     * @return: com.xiaoa.gossip.cluster.command.PingNodeState
     **/
    public static PingNodeState pingCommandToPingNodeState(PingCommand pingCommand) {
        PingNodeState nodeState = new PingNodeState();
        nodeState.setId(pingCommand.getId());
        nodeState.setIp(pingCommand.getMyip());
        nodeState.setPort(pingCommand.getPort());
        nodeState.setState(pingCommand.getState());
        nodeState.setSlot(pingCommand.getMyslots());
        nodeState.setLastPingTime(new Date());
        nodeState.setDataVersion(pingCommand.getDataVersion());
        nodeState.setMetadata(pingCommand.getMyMetadata());
//        BeanUtils.copyProperties(pingCommand , nodeState);

        return nodeState;
    }

    /**
     *  按照时间排序
     * @author jm008783
     * @param: [nodeStateList]
     * @return: void
     **/
    public static void doSortByTimeDesc(List<PingNodeState> nodeStateList) {

        Collections.sort(nodeStateList, ((o1, o2) -> o1.getLastPingTime().compareTo(o2.getLastPingTime())));

    }

    public static void doSortByTimeAsc(List<PingNodeState> nodeStateList) {

        Collections.sort(nodeStateList, ((o1, o2) -> o2.getLastPingTime().compareTo(o1.getLastPingTime())));

    }


}
